const formatTime = (date, symbol1, symbol2) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute, second].map(formatNumber).join(symbol2 || ':')
}

const formatTime_1 = (date, symbol1, symbol2) => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()

  return [month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute].map(formatNumber).join(symbol2 || ':')
}

const formatTime_2 = (date, symbol1, symbol2) => {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join(symbol1 || '/');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const dealObjectValue =(obj)=>{
    let param = {};
    console.log(obj)
    if ( obj === null || obj === undefined || obj === "" ) return param;
    for ( let key in obj ){
        if(typeof(obj[key]) === "Object" ){
            param[key] = dealObjectValue(obj[key]);
        }else if(  obj[key] !== null && obj[key] !== undefined && obj[key] !== ""  ){
            param[key] = obj[key];
        }
    }
    return param;
}

const testidCard = n => {
  if ((/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(n))){
    return false;
  }else{
    return true;
  }
}
const formatDate = date => {
  console.log(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('/')
}
const IdCard=(UUserCard, num)=>{
  if (num == 1) {    //获取出生日期    
    var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);    
    return birth; 
  }  
  if (num == 2) {    //获取性别    
    if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {      //男      
      return "男";    
    } else {      //女      
      return "女";    
    }
  }  
  if (num == 3) {    //获取年龄    
    var myDate = new Date();    
    var month = myDate.getMonth() + 1;    
    var day = myDate.getDate();    
    var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;    
    if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) { 
      age++;
    }
    return age;
  }
}

//进行经纬度转换为距离的计算
function Rad(d){
     return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
}

/*
 计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
 默认单位km
*/
const getMapDistance=(lat1,lng1,lat2,lng2)=>{
  var radLat1 = Rad(lat1);
  var radLat2 = Rad(lat2);
  var a = radLat1 - radLat2;
  var  b = Rad(lng1) - Rad(lng2);
  var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
  Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
  s = s *6378.137 ;// EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000; //输出为公里
  //s=s.toFixed(2);
  return s;
}


//获取倒计时
const InitTime=(endtime)=>{
  var dd,hh,mm,ss = null;
  var time = (new Date(endtime).getTime() - new Date().getTime())/1000;
  if(time<=0){
      return '结束'
  }else{
      dd = Math.floor(time / 60 / 60 / 24);
      hh = Math.floor((time / 60 / 60) % 24);
      mm = Math.floor((time / 60) % 60);
      ss = Math.floor(time  % 60);
      var str = dd+"天"+hh+":"+mm+":"+ss;
      return str;
  }
}



module.exports = {
  formatTime: formatTime,
  formatTime_1: formatTime_1,
  formatTime_2:formatTime_2,
  dealObjectValue,
  testidCard,
  formatDate,
  IdCard:IdCard,
  getMapDistance,
  InitTime:InitTime,
}